package cn.tedu.service.impl;

import cn.tedu.mapper.GradeCatMapper;
import cn.tedu.pojo.Grade;
import cn.tedu.pojo.GradeCat;
import cn.tedu.pojo.Student;
import cn.tedu.service.GradeCatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Service
public class GradeCatMapperImpl implements GradeCatService {
    @Autowired
    private GradeCatMapper gradeCatMapper;

    public Map<Integer,List<GradeCat>> getMap(){
        Map<Integer,List<GradeCat>> map = new HashMap<>();
        //查询所有的数据库记录
        List<GradeCat> list = gradeCatMapper.selectList(null);
        //1.遍历数据
        for(GradeCat itemCat:list){
            //获取parentId
            int parentId = itemCat.getParentId();
            if(map.containsKey(parentId)){  //判断集合中是否有key
                //表示数据存在,将自己追加
                map.get(parentId).add(itemCat);
            }else{
                //key不存在, 定义list集合,将自己作为第一个元素追加
                List<GradeCat> childrenList = new ArrayList<>();
                childrenList.add(itemCat);
                //将数据保存到map集合中
                map.put(parentId,childrenList);
            }
        }
        return map;
    }
    public List<GradeCat> getTwoList(Map<Integer,List<GradeCat>> map){
        //1.先查询一级菜单数据
        List<GradeCat> oneList = map.get(0);
        //2.遍历每个一级菜单去封装二级数据
        for(GradeCat oneItemCat : oneList){
            //parent_id = 一级ID
            int parentId = oneItemCat.getId();
            //查询二级数据
            List<GradeCat> twoList = map.get(parentId);
            oneItemCat.setChildren(twoList);
        }
        return oneList;
    }
    //该方法获取1-2-3级数据信息
    public List<GradeCat> getThreeList(Map<Integer, List<GradeCat>> map) {
        //因为二级菜单是封装在一级菜单里 所以这里是返回的一级菜单
        List<GradeCat> oneList = getTwoList(map);
        //遍历一级菜单
        for(GradeCat oneItemCat : oneList){
            List<GradeCat> twoList = oneItemCat.getChildren();
            //判断二级菜单是否为空
            if(twoList == null || twoList.size() == 0){//根据业务，if里的条件也要有顺序 null在前
                continue;//跳过本次循环继续执行下一次循环
            }
            //遍历二级菜单
            for (GradeCat twoItemCat : twoList){
                Integer parentId = twoItemCat.getId();
                List<GradeCat> threeList = map.get(parentId);//通过get（key）拿到value 拿到三级菜单
                //将三级菜单封装到二级菜单中
                twoItemCat.setChildren(threeList);
            }
        }
        return oneList;
    }
    @Override
    public List<GradeCat> findStudentList(Integer level) {
        long startTime = System.currentTimeMillis();
        //获取所有集合数据
        Map<Integer,List<GradeCat>> map = getMap();
        if(level == 1){
            //1.一级商品分类信息
            return map.get(0);
        }
        if(level == 2){ //获取一级菜单和二级菜单
            return getTwoList(map);
        }
        List<GradeCat> threeList =  getThreeList(map);
        long endTime = System.currentTimeMillis();
        System.out.println("耗时"+(endTime - startTime)+"毫秒");
        return threeList;
    }

    @Override
    @Transactional
    public void saveGradeCat(GradeCat gradeCat) {
        gradeCatMapper.insert(gradeCat);
    }
}
